<?php

class FUserGoldCoinNewLog extends BaseModel
{
    public $res;
    public $expire;

    public function getSource()
    {
        return "f_user_gold_coin_new_log";
    }

    public function page_list($params)
    {
        $page = isset($params['page']) ? $params['page'] : 1;
        $size = 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condition = '1=1 ';

        if (isset($params['phone']) && !empty($params['phone'])) {
            $userDb = new User();
            $info   = $userDb->findFirstByPhone($params['phone']);
            if (!$info) {
                return [
                    'count' => 0,
                    'total' => 0,
                    'list' => [],
                ];
            }
            $condition .= " and user_id = $info->id";
        }
        if (isset($params['start_time']) && !empty($params['start_time'])) {
            $condition .= " and created_at >= '{$params['start_time']}'";
        }

        if (isset($params['user_id']) && !empty($params['user_id'])) {
            $condition .= " and user_id = {$params['user_id']}";
        }

        if (isset($params['end_time']) && !empty($params['end_time'])) {
            $condition .= " and created_at <= '{$params['end_time']}'";
        }
        if(isset($params['type']) && !empty($params['type'])){
            $condition .= " and type in ({$params['type']})";
        }
        if(isset($params['status'])){
            $condition .= " and status = {$params['status']}";
        }
        $result['count'] = $this->countGold($condition);
        $result['total'] = $this::count($condition);
        $result['list']  = $this::find(
            array(
                $condition,
                'columns' => 'user_id,num,remarks,created_at,status',
                "order"  => 'id desc',
                'limit'  => $size,
                'offset' => $page
            ))->toarray();
        return $result;
    }

    public function countGold($condition){
        $where['conditions'] = $condition;
        $where['columns'] = "sum(num) as num";
        $num = $this->findFirst($where);
        return $num->num?$num->num:0;
    }
    public function sumNum($status = 1, $uid = 0)
    {
        $columns['conditions'] = 'type != 8 and status = ' . $status;
        if ($uid > 0) {
            $columns['conditions'] .= ' and user_id = ' . $uid;

        }
        $columns['column'] = 'num';
        return $this::sum($columns);
    }
    public function sumNumEmpty($uid = 0)
    {
        $columns['conditions'] = 'type in (7,8)';
        if ($uid > 0) {
            $columns['conditions'] .= ' and user_id = ' . $uid;

        }
        $columns['column'] = 'num';
        return $this::sum($columns);
    }

}
